﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using LoL.classes;

namespace LoL
{
    class Character : Actor
    {
        #region Attributes

        private int level;
        private int healthPoints;
        private int totalHealthPoints;
        private Vector2 attackSpeed;
        private int jumping;
        private Vector2 movementSpeed;

        #endregion

        #region Properties

        public float MovementSpeedX
        {
            get
            {
                return movementSpeed.X;
            }
            set
            {
                movementSpeed.X = value;
            }
        }

        public float MovementSpeedY
        {
            get
            {
                return movementSpeed.Y;
            }
            set
            {
                movementSpeed.Y = value;
            }
        }

        #endregion

        public Character(Vector2 startPosition, int level, Vector2 startSpeed, int totalHealth, Vector2 attackSpeed, int jumping) : base(startPosition) 
        {
            movementSpeed = startSpeed;
            this.level = level;
            totalHealthPoints = totalHealth;
            this.healthPoints = totalHealth;
            this.attackSpeed = attackSpeed;
            this.jumping = jumping;
        }


        public void Jump()
        {
            Position += new Vector2(0,-2);
            MovementSpeedY -= jumping;

        }

        /*
         * Override method for generating bounding rectangles for moving sprites.
         */ 
        public override Rectangle generateBoundingRectangle()
        {
            return new Rectangle((int) (Position.X + MovementSpeedX - Origin.X), 
                (int) (Position.Y + MovementSpeedY - Origin.Y + 1), // +1 er hack for å få det til å se bedre ut, vil fjernes etterhvert.
                (int)(this.getTexture().Width * Scale.X), 
                (int)(this.getTexture().Height * Scale.Y));
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            List<Collision> collisions = CollisionDetector.detectCollision(this);
            
            /*
             * Sjekk for å håndtere hopping.
             *
             */


            if (Position.Y < 600)// && collisions.Count <= 0)
            {
                if (MovementSpeedY < 20)
                    MovementSpeedY += 0.5f;
            }

            else if (Position.Y > 600)
            {
                Position = new Vector2(Position.X, 600);
                MovementSpeedY = 0;
            }


              
            foreach (Collision collision in collisions)
            {
                handleCollision(collision, gameTime);

            }
           

            Position = new Vector2(Position.X + MovementSpeedX, Position.Y + MovementSpeedY);


        
        }

       
        private void handleCollision(Collision collision, GameTime gameTime)
        {
            CollisionType collisionDirection = collision.getDirection();
            Actor collidingActor = collision.getCollidingActor();

            if (collisionDirection == CollisionType.collideRight)
            {
                MovementSpeedX = 0;
                //Position = new Vector2(collidingActor.PositionX - Width(), PositionY);
            }
            else if (collisionDirection == CollisionType.collideLeft)
            {
                MovementSpeedX = 0;
                //Position = new Vector2(collidingActor.PositionX + collidingActor.Width(), PositionY);
            }
            else if (collisionDirection == CollisionType.collideBottom)
            {
                MovementSpeedY = 0;
                //Position = new Vector2(PositionX, collidingActor.PositionY - Height() + 2);
                if (collidingActor is DroppingPlatform)
                    ((DroppingPlatform)collidingActor).activate(gameTime);
                if (collidingActor is SinkablePlatform)
                    ((SinkablePlatform)collidingActor).activate(gameTime); // problemer med å "deactivere" hmm..x   

            }
            if (collisionDirection == CollisionType.collideTop)
            {
                MovementSpeedY = 0;
                Position += new Vector2(0, 1);  
            }
        }




    }
}
